//
//  RecommentdContentViewLightMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-6-6.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "RecommentdContentViewLightMaker.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"
#import "Config.h"
#import "ProductContentViewLightMaker.h"
#import <QuartzCore/QuartzCore.h>

@implementation RecommentdContentViewLightMaker

+ (void)makePrototype:(UIView *)view
{
    //
    if (view) {
        //推荐内容
        UILabel* contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        contentLabel.backgroundColor = [UIColor clearColor];
        contentLabel.numberOfLines = 0;
        contentLabel.lineBreakMode = UILineBreakModeWordWrap;
        contentLabel.font = _SYS_FONT_002;
        [view addSubview:contentLabel];
        
        
        //商品信息的背景图片
        UIView* outlineView = [[UIView alloc] initWithFrame:CGRectMake(5, 27, 290, 40)];
        outlineView.layer.cornerRadius = 5;
        outlineView.layer.borderWidth = 1;
        outlineView.layer.borderColor = [_SYS_COLOR_005 CGColor];
        [view addSubview:outlineView];
        
        
        //商品信息view
        UIView* productView = [[UIView alloc] initWithFrame:CGRectMake(5, outlineView.frame.origin.x + 5, 280, 0)];
        productView.backgroundColor = [UIColor clearColor];
        [ProductContentViewLightMaker makePrototype:productView];
        [view addSubview:productView];
        
        
        //箭头
        UIImageView* arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 25, 13, 9)];
        arrowImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_icon_012@2x" ofType:@"png"]];
        //        arrowImageView.backgroundColor = [UIColor redColor];
        [view addSubview:arrowImageView];
    }
}


// 计算推荐动态的高度
// 高度为：推荐内容 + 产品视图 + 背景视图边框 * 2 + footer
+ (float)heightWithDictionary:(NSDictionary *)data
{
    float height = 0.0f;
    
    //推荐
    NSString* recommendContent = data[@"content"];
    if (recommendContent.length > 0) {
        
        CGSize size = [recommendContent sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 100.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += size.height > 20 ? size.height : 20;
    }
    
    height += [ProductContentViewLightMaker heightWithDictionary:data];
    
    //背景视图边框高度*2
//    height += 10 * 2;
    
    //间隙
    height += 5;
    
    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    //获取文字内容
    NSString* content = data[@"content"];
    //获取文字label
    UILabel* contentLabel = view.subviews[0];
    contentLabel.text = content ? content : @"";
    
    //由于文字部分高度不定，所以需要在cell重用后向cell中加入文字部分
    //先设置动态内容部分view的高度，再设置其子view的高度，即文字说明的高度
    CGSize size = [contentLabel.text sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 100.0f) lineBreakMode:UILineBreakModeWordWrap];
    contentLabel.frame = CGRectMake(contentLabel.frame.origin.x, contentLabel.frame.origin.y, 280, size.height);
    
    
    //商品信息视图
    UIView* productView = view.subviews[2];
    [ProductContentViewLightMaker drawContentView:productView withDictionary:data];
    productView.frame = CGRectMake(productView.frame.origin.x, contentLabel.frame.origin.x + contentLabel.frame.size.height + 5, productView.frame.size.width, productView.frame.size.height);
    
    //先重设背景图的frame
    UIView* outlineView = view.subviews[1];
    outlineView.frame = CGRectMake(outlineView.frame.origin.x, contentLabel.frame.origin.y + contentLabel.frame.size.height + 7, outlineView.frame.size.width, productView.frame.size.height + 10);
    
    //重画箭头的位置，根据outlineView来确定位置坐标
    UIImageView* arrowImageView = view.subviews[3];
    arrowImageView.frame = CGRectMake(20, outlineView.frame.origin.y - 8, arrowImageView.frame.size.width, arrowImageView.frame.size.height);
        
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, outlineView.frame.origin.y + outlineView.frame.size.height);
}

@end
